<?php get_instance()->view('common/header');?>
<body >
<div class="page">
    <div class="page__bd">
        <div class="weui-search-bar" id="searchBar">
            <div class="weui-search-bar__form">
                <div class="weui-search-bar__box">
                    <i class="weui-icon-search"></i>
                    <input type="search" class="weui-search-bar__input" id="searchInput" placeholder="搜索" required/>
                    <a href="javascript:" class="weui-icon-clear" id="searchClear"></a>
                </div>
                <label class="weui-search-bar__label" id="searchText">
                    <i class="weui-icon-search"></i>
                    <span>搜索</span>
                </label>
            </div>
            <a href="javascript:" class="weui-search-bar__cancel-btn" id="searchCancel">取消</a>
        </div>
        <div class="weui-cells searchbar-result" id="searchResult">

        </div>
    </div>
</div>
<div class="div1 mt5">
    <div class="hotserchtle m0a wp94 p5_0">
        <h1 class="f09 cor6 f400">热门搜索 top10</h1>
    </div>
    <div class=" henglineleft vCenter wp94 m0a search">
        <?php if (!empty($hot)): foreach ($hot as $k => $v):?>
            <b class="hotserchbtn"><?php echo $v?></b>
        <?php endforeach; endif;?>
    </div>
</div>
<div class="div1 mt5">
    <div class="hotserchtle m0a wp94 p5_0">
        <h1 class="f09 cor6 f400">最近搜索 最近10条
            <div href="<?php echo getUrl('home/unSearch')?>" alert-message="是否确定删除最近搜索记录？" class="ajax-request-btn fr">
                <img class="heightimg" src="<?php echo $this->staticUrl?>img/png/pic93.png" alt="">
            </div>
        </h1>
    </div>
    <div class=" henglineleft vCenter wp94 m0a search">
        <?php if (!empty($search)): foreach ($search as $k => $v):?>
            <b class="hotserchbtn"><?php echo $v?></b>
        <?php endforeach; endif;?>
    </div>
</div>
</body>
<script src="<?php echo $this->staticUrl?>js/base.js"></script>
<script type="text/javascript">
    $(function(){
        var $searchBar = $('#searchBar'),
            $searchResult = $('#searchResult'),
            $searchText = $('#searchText'),
            $searchInput = $('#searchInput'),
            $searchClear = $('#searchClear'),
            $searchCancel = $('#searchCancel'),
            $search = $('.search'),
            merchantId = "<?php echo $merchantId?>";

        function hideSearchResult(){
            $searchResult.hide();
            $searchInput.val('');
        }
        function cancelSearch(){
            hideSearchResult();
            $searchBar.removeClass('weui-search-bar_focusing');
            $searchText.show();
            $(".div1").show();
        }

        $("#searchInput").keypress(function (event) {
            if(event.keyCode == "13") {
                document.activeElement.blur();
                $('.search').trigger('click');
            }
        })

        $searchText.on('click', function(){
            $searchBar.addClass('weui-search-bar_focusing');
            $searchInput.focus();
        });
        $searchInput.on('blur', function () {
                if(this.value.length){
                    $searchResult.show();
                    getList(this.value, false);
                } else{
                    cancelSearch();
                    $searchResult.hide();
                }
            });

        $searchClear.on('click', function(){
            hideSearchResult();
            $searchInput.focus();
        });
        $searchCancel.on('click', function(){
            cancelSearch();
            $searchInput.blur();
        });

        $search.on('click', 'b', function () {
            $searchResult.show();
            getList($(this).html(), true);
        });

        /**
         * 返回搜索内容
         */
        function getList(keyword, search){
            blockUI.show('搜索中...');
            var imgS = "<?php echo $this->staticUrl?>img/default/6.png";console.log(search);
            $.ajax('<?php echo getUrl('home/searchByKeyword');?>', {
                dataType:'json',
                type:'post',
                data:{keyword:keyword,search:search,merchant_id:merchantId},
                error:function(){
                    blockUI.hide();
                    show_message({status:0, message:'请求超时'});
                },
                timeout:60000,
                success:function(e,textStatus,jqXHR){
                    blockUI.hide();
                    show_message(e);
                    if (e.status == 1) {
                        $searchResult.html('');
                        var a = each(e.data.product, 2);
                        var b = each(e.data.shop, 1);
                        if (!a && !b){
                            $searchResult.html('<div class="statebox chuizhi jdjz" style="flex: 1;height: 63%;">' +
                                '<img class="stateimgda mb5" src="'+imgS+'" alt="">' +
                                '<span class="f09 cor9 tc mb20">主人，暂无搜索结果~</span>' +
                                '</div>')
                        }
                        $(".div1").hide();
                        if (!search) {
                            var search = '';
                            $.each(eval(e.data.search), function (k, v) {
                                search += '<b class="hotserchbtn">' + v + '</b>';
                            });
                            $(".search").html(search);
                        }
                    }
                }
            });
        }

        function each(list, t) {
            var dImg = "<?php echo $this->staticUrl?>img/png/pic25.png",
                sImg = "<?php echo $this->staticUrl?>img/png/pic23.png",
                dUrl = "<?php echo getUrl('product/merchant?merchant_id=')?>",
                sUrl = "<?php echo getUrl('product/info?merchant_id=')?>";
            var html = '',
                imgHtml = '',
                pHtml = '';
            if (t == 1){
                imgHtml = '<img src="'+dImg+'" alt="">';
                pHtml = 'address';
            }else{
                imgHtml = '<img src="'+sImg+'" alt="">';
                pHtml = 'name';
            }
            if (list.length) {
                $.each(list, function (key, obj) {
                    var url;
                    if (t == 1) {
                        url = dUrl + obj.id;
                    } else {
                        url = sUrl + obj.shop_id + '&product_id=' + obj.id;
                    }
                    html =
                        '<a href="' + url + '"><div class="weui-cell">' +
                        '   <div class="weui-cell__bd weui-cell_primary">' +
                        '       <div class="serbox vCenter hengleft">' +
                        '           <div class="imgbox mr10 oh" style="width: 2em;height: 2em;">' + imgHtml + '</div>' +
                        '           <div class=""><h1>' + obj.shop_name + '</h1>' +
                        '               <p class="cor9 f08">' + obj[pHtml] + '</p>' +
                        '           </div>' +
                        '       </div>' +
                        '   </div>' +
                        '   <div class="weui-cell__ft cor9 f07">' + obj.distance + '</div>' +
                        '</div></a>';
                    $searchResult.append(html);
                });
                return true;
            }else{
                return false;
            }
        }
    });

    function ajaxRequestCallBack() {
        $(".search").html('');
    }
</script>
</html>